package vista.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;

import cron.CronBolsa;


public class CronBolsaServlet extends HttpServlet{

	private static final long serialVersionUID = -3612124788393545558L;

	private Scheduler sched;

	private static boolean cronEnEjecucion = false;
	
	private static final String EXPRESION_QUARTZ = "0 22 15 * * ?";
	
	private static final String NOMRE_CRON_BOLSA = "CronBolsa";
	
	public void init(ServletConfig servlet) throws ServletException 
	{
		try {    			
			JobDetail bolsaJob = new JobDetail(NOMRE_CRON_BOLSA, Scheduler.DEFAULT_GROUP, CronBolsa.class);
			cronEnEjecucion = true;
			CronTrigger triggerCreate = new CronTrigger(NOMRE_CRON_BOLSA,Scheduler.DEFAULT_GROUP);			
			sched = StdSchedulerFactory.getDefaultScheduler();
			sched.start();
			triggerCreate.setCronExpression(EXPRESION_QUARTZ);	   	  
			sched.scheduleJob(bolsaJob, triggerCreate);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void destroy() 
	{
		try{
			if(cronEnEjecucion){
				sched.deleteJob(NOMRE_CRON_BOLSA,Scheduler.DEFAULT_GROUP);
				cronEnEjecucion = false;
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			
		}
	}
}
